package model

import (
	"time"

	"gorm.io/gorm"
)

// User user model
type User struct {
	ID        uint           `json:"id" gorm:"primarykey"`                    // user id
	Username  string         `json:"username" gorm:"uniqueIndex;size:50"`     // username
	Email     string         `json:"email" gorm:"uniqueIndex;size:100"`       // email
	Phone     string         `json:"phone" gorm:"uniqueIndex;size:20"`        // phone number
	Password  string         `json:"-" gorm:"size:255"`                       // password (not returned to frontend)
	Nickname  string         `json:"nickname" gorm:"size:50"`                  // nickname
	Avatar    string         `json:"avatar" gorm:"size:255"`                  // avatar URL
	Role      string         `json:"role" gorm:"size:20;default:'user'"`      // user role: user, admin
	Status    int            `json:"status" gorm:"default:1"`                 // user status: 1-active, 0-disabled
	LastLogin *time.Time     `json:"last_login"`                              // last login time
	CreatedAt time.Time      `json:"created_at"`                              // created time
	UpdatedAt time.Time      `json:"updated_at"`                              // updated time
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`                          // soft delete time
}

// TableName specify table name
func (User) TableName() string {
	return "users"
}

// UserRegisterRequest user registration request
type UserRegisterRequest struct {
	Username string `json:"username" binding:"required,min=3,max=20"` // username
	Email    string `json:"email" binding:"required,email"`           // email
	Phone    string `json:"phone" binding:"required,len=11"`          // phone number
	Password string `json:"password" binding:"required,min=6,max=20"` // password
	Nickname string `json:"nickname" binding:"required,max=50"`       // nickname
	Code     string `json:"code" binding:"required,len=6"`            // verification code
}

// UserLoginRequest user login request
type UserLoginRequest struct {
	Username string `json:"username" binding:"required"` // username/email/phone
	Password string `json:"password" binding:"required"` // password
}

// UserLoginResponse user login response
type UserLoginResponse struct {
	Token string `json:"token"` // JWT token
	User  User   `json:"user"`  // user info
}

// UserUpdateRequest user update request
type UserUpdateRequest struct {
	Nickname string `json:"nickname" binding:"max=50"` // nickname
	Avatar   string `json:"avatar" binding:"max=255"`  // avatar URL
}

// UserProfileResponse user profile response
type UserProfileResponse struct {
	ID        uint       `json:"id"`         // user id
	Username  string     `json:"username"`   // username
	Email     string     `json:"email"`      // email
	Phone     string     `json:"phone"`      // phone number
	Nickname  string     `json:"nickname"`   // nickname
	Avatar    string     `json:"avatar"`     // avatar URL
	Role      string     `json:"role"`       // user role
	Status    int        `json:"status"`     // user status
	LastLogin *time.Time `json:"last_login"` // last login time
	CreatedAt time.Time  `json:"created_at"` // created time
}

// UserListResponse user list response
type UserListResponse struct {
	Total int64                `json:"total"` // total count
	Users []UserProfileResponse `json:"users"` // user list
}

// BeforeCreate GORM hook: before create
func (u *User) BeforeCreate(tx *gorm.DB) error {
	// set default role
	if u.Role == "" {
		u.Role = "user"
	}
	
	// set default status
	if u.Status == 0 {
		u.Status = 1
	}
	
	return nil
}

// ToProfileResponse convert to user profile response
func (u *User) ToProfileResponse() UserProfileResponse {
	return UserProfileResponse{
		ID:        u.ID,
		Username:  u.Username,
		Email:     u.Email,
		Phone:     u.Phone,
		Nickname:  u.Nickname,
		Avatar:    u.Avatar,
		Role:      u.Role,
		Status:    u.Status,
		LastLogin: u.LastLogin,
		CreatedAt: u.CreatedAt,
	}
} 